﻿using System;
using System.Configuration;
using System.Web;
using System.Web.UI;
using System.Web.UI.HtmlControls;
using System.Web.UI.WebControls;
using System.IO;

public partial class site_log_LogReader : System.Web.UI.Page
{
    protected string startPath = "";

    protected string physicalPath = "";
    protected string virtualParentPath = "";

    protected void Page_Load(object sender, EventArgs e)
    {
        if (!IsPostBack)
        {
            bindData();
        }
    }
   

    
    

   

    

    



    /// <summary>
    /// 绑定列表
    /// </summary>
    private void bindData()
    {
        initPath();
        GetList(physicalPath);
        litCurrentDir.Text = startPath;
    }

    /// <summary>
    /// 初始化路径
    /// </summary>
    private void initPath()
    {

        physicalPath = FtpServer.MapPath("");
        virtualParentPath = startPath;
        if (startPath == ".")
        {
            startPath = "";
        }


        if (Request.QueryString["path"] != null)
        {
            string path = Request.QueryString["path"].Trim();
            hfPath.Value = path;

            if (!string.IsNullOrEmpty(path) && path != "/")
            {
                startPath = path + "/";
                virtualParentPath = getParentPath(path);
                physicalPath = FtpServer.MapPath(startPath);
            }
        }

    }

    private void GetList(string path)//列出当前目录下的文件
    {
        string[] dirs = Directory.GetDirectories(path);
        rptDir.DataSource = dirs;
        rptDir.DataBind();

        DirectoryInfo directory = new DirectoryInfo(path);
        FileInfo[] files = directory.GetFiles();
        rptFile.DataSource = files;
        rptFile.DataBind();
    }
    private string getParentPath(string path)
    {
        string strPath = path;
        if (strPath.LastIndexOf("/") >= 0)
            return strPath.Substring(0, strPath.LastIndexOf("/"));
        return "";
    }


    protected string formatDate(object o)
    {
        if (Convert.IsDBNull(o))
            return "-";
        return Convert.ToDateTime(o).ToString("yyyy-MM-dd HH:mm:ss");
    }


    protected string getDirName(string fullDir)
    {
        DirectoryInfo di = new DirectoryInfo(fullDir);

        fullDir = di.Name;
        return fullDir;
    }

    /// <summary>
    /// 递归删除
    /// </summary>
    /// <param name="path"></param>
    private void recursiveDel(string path)
    {
        DeleteFiles(path);//删除当前目录下的文件
        if (hasSubDir(path))
        {
            getSubDir(path);
        }
        Directory.Delete(path);
        return;
    }

    private void DeleteFiles(string path)//删除当前目录下的文件
    {

        FileInfo[] files = new DirectoryInfo(path).GetFiles();
        foreach (FileInfo _f in files)
        {
            File.Delete(_f.FullName);
        }
    }

    private void getSubDir(string path)//删除当前目录下的所有子目录下的文件
    {
        string[] dirs = Directory.GetDirectories(path);
        for (int i = 0; i < dirs.Length; i++)
        {
            recursiveDel(dirs[i]);
        }
    }

    private bool hasSubDir(string path)//判断是否有子目录
    {
        return Directory.GetDirectories(path).Length > 0;
    }

    protected void lbLogout_Click(object sender, EventArgs e)
    {
        Session["U"] = null;
        Session.Clear();

        //this.Response.Redirect("Login.aspx");
    }
}